define(['jquery', 'bootstrap', 'backend', 'table', 'form','upload'], function ($, undefined, Backend, Table, Form,Upload) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluedu/course/index' + location.search,
                    add_url: 'xiluedu/course/add',
                    edit_url: 'xiluedu/course/edit',
                    del_url: 'xiluedu/course/del',
                    multi_url: 'xiluedu/course/multi',
                    import_url: 'xiluedu/course/import',
                    file_url: 'xiluedu/course_file/index',
                    dragsort_url:'',
                    table: 'xiluedu_course',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                console.log(form)
                $("input[name='category_id']", form).addClass("selectpage").data("source", "xiluedu/category/selectpage?isTree=1");
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                search:false,
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'category_id', title: __('Category_id'),visible:false},
                        {field: 'category.name', title: __('Category_id'),operate:false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'tag_ids', title: __('Tag_ids'), searchList: Config.tags,formatter: Table.api.formatter.flag},
                        {field: 'teacher_id', title: __('Teacher_id'),searchList: Config.teachers,formatter: Table.api.formatter.normal},
                        {field: 'thumb_image', title: __('Thumb_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'salesprice', title: __('Salesprice'), operate:false},
                        {field: 'market_price', title: __('Market_price'), operate:false},
                        {field: 'is_charge', title: __('Is_charge'), searchList: {"0":__('Is_charge 0'),"1":__('Is_charge 1'),"2":__('Is_charge 2')}, formatter: Table.api.formatter.normal},
                        {field: 'is_hot', title: __('Is_hot'), searchList: {"0":__('Is_hot 0'),"1":__('Is_hot 1')}, formatter: Table.api.formatter.normal},
                        {field: 'is_new', title: __('Is_new'), searchList: {"0":__('Is_new 0'),"1":__('Is_new 1')}, formatter: Table.api.formatter.normal},
                        {field: 'real_num', title: __('Real_num'),operate:false},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'collection_count', title: __('Collection_count'),sortable:true, operate: false},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'),buttons: [
                                {
                                    name: 'file',
                                    text: __('章节'),
                                    title: __('章节'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-step',
                                    url: function (row) {
                                        return $.fn.bootstrapTable.defaults.extend.file_url+"?course_id="+row.id
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.type==3 ? false : true;
                                    }
                                },
                                {
                                    name: 'comment',
                                    title: __('评价'),
                                    //text: __('实名认证'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-commenting-o',
                                    url: function (row) {
                                        return Fast.api.fixurl("xiluedu/course_comment/index?course_id="+row.id)
                                    },
                                    visible: function (row) {
                                        return true;
                                    }
                                },], table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'xiluedu/course/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'xiluedu/course/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'xiluedu/course/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();

        },
        edit: function () {
            Controller.api.bindevent();
        },

        selects: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluedu/course/selects' + location.search,
                    dragsort_url: "",
                    table: 'xilushop_goods',
                }
            });

            var table = $("#table");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='category_id']", form).addClass("selectpage").data("source", "xiluedu/category/selectpage?isTree=1");
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                search:false,
                fixedColumns: true,
                fixedRightNumber: 1,
                maintainSelected: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'thumb_image', title: __('Thumb_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'category_id', title: __('Category_id'),visible:false},
                        {field: 'category.name', title: __('Category_id'),operate:false},
                        {field: 'collection_count', title: __('Collection_count'),sortable:true, operate: false},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate', title: __('Operate'), width: 85, events: {
                                'click .btn-chooseone': function (e, value, row, index) {
                                    Fast.api.close({data: [row]});
                                },
                            }, formatter: function () {
                                return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
                            }
                        }
                    ]
                ]
            });

            // 选中多个
            $(document).on("click", ".btn-choose-multi", function () {
                var data =  Table.api.selecteddata(table);
                if(data.length<=0){
                    Toastr.error('请选择至少一件商品');
                    return false;
                }
                Fast.api.close({data: data});
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            var options = table.bootstrapTable('getOptions');
            var selectedIds = [];
            if (new URLSearchParams(location.search).get('ids')) {
                selectedIds = new URLSearchParams(location.search).get('ids').split(',');
                for (var i=0;i<selectedIds.length;i++){
                    selectedIds[i] = parseInt(selectedIds[i]);
                }
                options.selectedIds = selectedIds;
                options.selectedData = Config.selectdata;
            }
        },
        api: {
            bindevent: function () {
                $(document).on('change','#c-type',function () {
                    var value = $(this).val();
                    $(".selected1,.selected2,.selected3").addClass("hide");
                    $(".selected"+value).removeClass("hide");
                })
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
